package com.linkstec.util;

import java.util.Locale;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author linkage
 *
 */
@Component("springContextUtil")
public class SpringContextUtil implements ApplicationContextAware {
	private static ApplicationContext context = null;

	/**
	 * 
	 * @Title: setApplicationContext
	 * 
	 * @Description: spring获取bean工具类
	 * 
	 * @param applicationContext
	 * 
	 * @throws BeansException
	 * 
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext
	 *      (org.springframework.context.ApplicationContext)
	 */
	@SuppressWarnings("static-access")
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.context = applicationContext;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String beanName) {
		return (T) context.getBean(beanName);
	}

	/**
	 * 
	 * @param requiredType
	 * @return
	 */
	public static String[] getBeanNamesForType(Class<?> requiredType) {
		return context.getBeanNamesForType(requiredType);
	}

	public static String getMessage(String key) {
		return context.getMessage(key, null, Locale.getDefault());
	}

}
